package com.xiaoxie.common.utils;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * AES加解密工具类
 * 这个类主要是用来处理前端传过来的加密数据，比如：动态数据源的密码
 */
public class AESUtils {
    public static String decrypt(String content, String key) {
        try {
            byte[] encrypted1 = Base64.getDecoder().decode(content);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(key.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            // 去掉补充的字符
            String originalString = new String(original).trim();
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
